package com.songlanyun.modules.marketing.seckill.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.goods.model.vo.ActivitySkuComItemVo;
import com.songlanyun.modules.goods.model.vo.SkuComItemVo;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyGoodsSku;
import com.songlanyun.modules.marketing.seckill.dao.SeckillGoodsSkuDao;
import com.songlanyun.modules.marketing.seckill.entity.SeckillGoodsSku;
import com.songlanyun.modules.marketing.seckill.service.SeckillGoodsSkuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;

@Service("seckillGoodsSkuService")
public class SeckillGoodsSkuServiceImpl extends ServiceImpl<SeckillGoodsSkuDao, SeckillGoodsSku> implements SeckillGoodsSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillGoodsSku> page = this.page(
                new Query<SeckillGoodsSku>().getPage(params),
                new QueryWrapper<SeckillGoodsSku>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillGoodsSku> getBySeckillGoodsId(Long seckillGoodsId) {

        return this.baseMapper.selectList(
                new LambdaQueryWrapper<SeckillGoodsSku>()
                        .eq(SeckillGoodsSku::getSeckillGoodsId, seckillGoodsId)
        );
    }

    @Override
    public void deleteBySeckillGoodsId(Long seckillGoodsId) {
        this.baseMapper.delete(
                new LambdaQueryWrapper<SeckillGoodsSku>()
                        .eq(SeckillGoodsSku::getSeckillGoodsId, seckillGoodsId)
        );
    }

    @Override
    public SeckillGoodsSku getBySeckillGoodsAndSkuHashCode(Long seckillGoodsId, String skuHashCode) {
        SeckillGoodsSku sku = this.getOne(new LambdaQueryWrapper<SeckillGoodsSku>()
                .eq(SeckillGoodsSku::getSeckillGoodsId, seckillGoodsId)
                .eq(SeckillGoodsSku::getSkuHashCode, skuHashCode)
                .last("limit 1")
        );
        return sku;
    }

    @Override
    public List<ActivitySkuComItemVo> loadSkuItemList(Long id) {
        return this.baseMapper.loadSkuItemList(id);
    }

    @Override
    public List<SeckillGoodsSku> listByGoodIds(Long seckillGoodsId) {
        return this.baseMapper.selectList(
                new LambdaQueryWrapper<SeckillGoodsSku>()
                        .eq(SeckillGoodsSku::getSeckillGoodsId, seckillGoodsId)
                        .orderByAsc(SeckillGoodsSku::getCreateTime)
        );
    }

}
